<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>供货列表</title>
</head>
<style type="text/css">
   tbody {
      text-align: center;

   }

   caption {
      font-weight: 700;
      font-size: 18px;
   }

   .huise {
      height: 20px;
      width: 20px;
      position: absolute;


   }

   .jiuse {
      height: 20px;
      width: 20px;
      position: absolute;


   }


</style>

<body>
<script type="text/javascript">
      var tag = 1;
      function sortNumberAS(a, b) {
         return a - b
      }
      function sortNumberDesc(a, b) {
         return b - a
      }

      function SortTable(obj) {
         var td0s = document.getElementsByName("td0");
         var td1s = document.getElementsByName("td1");
         var td2s = document.getElementsByName("td2");
         var td3s = document.getElementsByName("td3");
         var tdArray0 = [];
         var tdArray1 = [];
         var tdArray2 = [];
         var tdArray3 = [];
         for (var i = 0; i < td0s.length; i++) {
            tdArray0.push(td0s[i].innerHTML);
         }
         for (var i = 0; i < td1s.length; i++) {
            tdArray1.push(parseInt(td1s[i].innerHTML));
         }
         for (var i = 0; i < td2s.length; i++) {
            tdArray2.push(parseInt(td2s[i].innerHTML));
         }
         for (var i = 0; i < td3s.length; i++) {
            tdArray3.push((td3s[i].innerHTML));
         }
         var tds = document.getElementsByName("td" + obj.id.substr(2, 1));
         var columnArray = [];
         for (var i = 0; i < tds.length; i++) {
            columnArray.push(parseInt(tds[i].innerHTML));
         }
         var orginArray = [];
         for (var i = 0; i < columnArray.length; i++) {
            orginArray.push(columnArray[i]);
         }
         if (obj.className == "as") {
            columnArray.sort(sortNumberAS);               //排序后的新值
            obj.className = "desc";
         } else {
            columnArray.sort(sortNumberDesc);               //排序后的新值
            obj.className = "as";
         }


         for (var i = 0; i < columnArray.length; i++) {
            for (var j = 0; j < orginArray.length; j++) {
               if (orginArray[j] == columnArray[i]) {
                  document.getElementsByName("td0")[i].innerHTML = tdArray0[j];
                  document.getElementsByName("td1")[i].innerHTML = tdArray1[j];
                  document.getElementsByName("td2")[i].innerHTML = tdArray2[j];
                  document.getElementsByName("td3")[i].innerHTML = tdArray3[j];
                  orginArray[j] = null;
                  break;
               }
            }
         }
      }


</script>
<table align="center" width="600" height="350" border="1" bordercolor="#999999" rules="all"
       cellpadding="3"
       cellspacing="0">
    <caption>供货列表</caption>
    <tbody>

    <tr>
        <th>商品</th>
        <th id="th1" onclick="SortTable(this)" class="as">价格&nbsp; <img src="./img/orange.png"
                                                                        class="jiuse"/></th>
        <th id="th2" onclick="SortTable(this)" class="as">数量<img src="./img/gray.png"
                                                                 class="huise"/>
        </th>
        <th id="th3" onclick="SortTable(this)" class="as">供货商</th>
    </tr>

    <tr>
        <td name="td0">发动机A</td>
        <td name="td1">23000</td>
        <td name="td2">11</td>
        <td name="td3">发动机供货商A</td>
    </tr>

    <tr>
        <td name="td0">发动机B</td>
        <td name="td1">26000</td>
        <td name="td2">15</td>
        <td name="td3">发动机供货商B</td>
    </tr>

    <tr>
        <td name="td0">引擎A</td>
        <td name="td1">15000</td>
        <td name="td2">17</td>
        <td name="td3">引擎供货商A</td>
    </tr>

    <tr>
        <td name="td0">引擎B</td>
        <td name="td1">16000</td>
        <td name="td2">19</td>
        <td name="td3">引擎供货商B</td>
    </tr>

    <tr>
        <td name="td0">制动器B</td>
        <td name="td1">22000</td>
        <td name="td2">22</td>
        <td name="td3">制动器供货商B</td>
    </tr>

    <tr>
        <td name="td0">制动器A</td>
        <td name="td1">25000</td>
        <td name="td2">24</td>
        <td name="td3">制动器供货商A</td>
    </tr>


    <tr>
        <td name="td0">主板A</td>
        <td name="td1">33000</td>
        <td name="td2">33</td>
        <td name="td3">主板供货商A</td>
    </tr>


    <tr>
        <td name="td0">主板B</td>
        <td name="td1">34000</td>
        <td name="td2">44</td>
        <td name="td3">主板供货商B</td>
    </tr>

    <tr>
        <td name="td0">涡轮A</td>
        <td name="td1">26000</td>
        <td name="td2">27</td>
        <td name="td3">涡轮供货商A</td>
    </tr>

    <tr>
        <td name="td0">涡轮B</td>
        <td name="td1">27000</td>
        <td name="td2">28</td>
        <td name="td3">涡轮供货商B</td>
    </tr>

    <tr>
        <td name="td0">尾气箱A</td>
        <td name="td1">30000</td>
        <td name="td2">30</td>
        <td name="td3">尾气箱供货商A</td>
    </tr>

    <tr>
        <td name="td0">尾气箱B</td>
        <td name="td1">31000</td>
        <td name="td2">31</td>
        <td name="td3">尾气箱供货商B</td>
    </tr>
    </tbody>
</table>
</body>

</html>